#include "pauseview.h"
#include "toolkit.h"
#include "item/imageitem.h"
#include <QMouseEvent>
#include <QLocale>
#include "view/hintview.h"
#include "resources.h"

enum PauseBtnTp
{
    CONTINUE_SBTP,
    RESTART_SBTP,
    MAIN_SBTP,
    NULL_SBTP,
}pauseBtnTp(NULL_SBTP);

PauseView::PauseView(QWidget *parent) :
    QGraphicsView(parent),
    m_hintview(NULL)
{
    m_Scene = new QGraphicsScene(this);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(m_Scene);
    m_Scene->setSceneRect(0.0, 0.0, 640.0, 360.0);
    setGeometry(0.0,0.0,640.0,360.0);

    m_hintItem = new ImageItem(ToolKit::m_rc->m_hintImage,
                           QRect(QPoint(0,0),ToolKit::m_rc->m_hintImage.size()));
    m_hintItem->setZValue(1.0);
    m_hintItem->setVisible(false);
    m_Scene->addItem(m_hintItem);

    switch(ToolKit::m_language)
    {
    case QLocale::Chinese:
    {
        m_bgItem = new ImageItem(QImage(":/image/pause/pause1.png"),
                                 QRect(QPoint(0,0),QImage(":/image/pause/pause1.png").size()));
    }
        break;
    case QLocale::HongKong:
    case QLocale::Taiwan:
    {
        m_bgItem = new ImageItem(QImage(":/image/pause/pause2.png"),
                                 QRect(QPoint(0,0),QImage(":/image/pause/pause2.png").size()));
    }
        break;
    default:
    {
        m_bgItem = new ImageItem(QImage(":/image/pause/pause3.png"),
                                 QRect(QPoint(0,0),QImage(":/image/pause/pause3.png").size()));
    }
        break;
    }
    m_bgItem->setPos(0.0,0.0);
    m_bgItem->setZValue(0.0);
    m_Scene->addItem(m_bgItem);
}

void PauseView::mousePressEvent(QMouseEvent *event)
{
    if(QRectF(260,100,130,30).contains(event->pos()))
    {
        if(ToolKit::m_bVoice)
            ToolKit::playSound(4);

        pauseBtnTp = CONTINUE_SBTP;
        m_hintItem->setPos(237.0,102.0);
        m_hintItem->setVisible(true);
    }
    else if(QRectF(260,140,130,30).contains(event->pos()))
    {
        if(ToolKit::m_bVoice)
            ToolKit::playSound(4);

        pauseBtnTp = RESTART_SBTP;
        m_hintItem->setPos(237.0,145.0);
        m_hintItem->setVisible(true);
    }
    else if(QRectF(260,180,130,30).contains(event->pos()))
    {
        if(ToolKit::m_bVoice)
            ToolKit::playSound(4);

        pauseBtnTp = MAIN_SBTP;
        m_hintItem->setPos(237.0,188.0);
        m_hintItem->setVisible(true);
    }
}

void PauseView::mouseReleaseEvent(QMouseEvent *event)
{
    m_hintItem->setVisible(false);
    switch(pauseBtnTp)
    {
    case CONTINUE_SBTP:
    {
        if(QRectF(260,100,130,30).contains(event->pos()))
        {
            emit gamecontinue();
        }
    }
        break;
    case RESTART_SBTP:
    {
        if(QRectF(260,140,130,30).contains(event->pos()))
        {
            emit gamerestart();
        }
    }
        break;
    case MAIN_SBTP:
    {
        if(QRectF(260,180,130,30).contains(event->pos()))
        {
            createHintView();
        }
    }
        break;
    default:
        break;
    }

    pauseBtnTp = NULL_SBTP;
}

void PauseView::createHintView()
{
    if(m_hintview == NULL)
    {
        m_hintview = new HintView(this);
        m_hintview->m_bGameBack = true;
        m_hintview->showFullScreen();
        connect(m_hintview,SIGNAL(hint(int)),this,SLOT(hintback(int)));
    }
}

void PauseView::delHintView()
{
    if(m_hintview != NULL)
    {
        delete m_hintview;
        m_hintview = NULL;
    }
}

void PauseView::hintback(const int &ihint)
{
    delHintView();
    if(ihint == 2)
    {
        emit gameback();
    }
}
